package edu.nju;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * format : dir/.../*
 */
public class WildEntry extends Entry{
    public WildEntry(String classpath) {
        super(classpath);
    }

    @Override
    public byte[] readClassFile(String className) throws IOException {
    	char[] cs=this.classpath.toCharArray();
    	String tempp="";
    	for (int i=0;i<cs.length-2;i++)
    		tempp=tempp+cs[i];
    	File file=new File(tempp);
    	File[] files = file.listFiles();
    	for (int i=0;i<files.length;i++) {
    		if (files[i].getPath().endsWith("jar")||files[i].getPath().endsWith("JAR")) {
    			
    			String jarName =files[i].getPath();
    	        JarFile jarFile = new JarFile(jarName);
    	        
    			Enumeration<JarEntry> entrys = jarFile.entries();
    			while (entrys.hasMoreElements()) {
    				JarEntry jarEntry = entrys.nextElement();
    				String temp=jarEntry.getName();
    				temp = IOUtil.transform(temp);
    				if (temp.contains(className)) {
    				   	try {
    				   		InputStream inputStream =jarFile.getInputStream(jarEntry);
    				        return IOUtil.readFileByBytes(inputStream);}
    				    	catch(IOException e) {
    				    		return null;
    				}
    			}
    			}
    		}
    	}
    	return null;
    }
}
